package com.supermap.filetransfer.thread;

import com.supermap.filetransfer.entity.DownloadEntity;
import com.supermap.filetransfer.enums.WorkState;
import com.supermap.filetransfer.network.listeners.IHttpRequestListener;
import com.supermap.filetransfer.network.utils.HttpUtils;

import java.io.IOException;
import java.util.Map;
import java.util.UUID;

import okhttp3.Call;
import okhttp3.Response;

/**
 * @author Huang.MinJie
 * @desc 下载线程类（颗粒线程作为最小的存在）
 * @date 2022/6/19 21:24
 */
public class DownloadThread extends Thread {
    public Response response;
    private DownloadEntity entity;
    private WorkState isSuccess = WorkState.NotComplete; // NotComplete.未完成 Success.下载成功 Fail.下载失败
    private IHttpRequestListener listener;
    public int reTryNum = 1;// 重新下载
    private Map<String,String> headers;


    @Override
    public void run() {
        super.run();
        if (entity == null || isNull(entity.url)) {
            return;
        }
        if (listener == null) {
            return;
        }
        HttpUtils.download(entity.url,entity.dirFile,entity.fileName,listener,headers);
    }

    /**
     * 空参数构造器一般用于 build()方式的构建   建议不要修改 权限标识
     */
    private DownloadThread() {
        super();
    }
    public DownloadThread(DownloadEntity entity, IHttpRequestListener listener,Map<String,String> headers) {
        this.entity = entity;
        this.listener = listener;
        this.headers = headers;
    }

    private boolean isNotEntity(String contents) {
        return contents != null && !"".equals(contents.trim());
    }

    private void setReTryNum(int reTryNum) {
        this.reTryNum = reTryNum;
    }

    private void setEntity(DownloadEntity entity) {
        this.entity = entity;
    }

    private void setListener(IHttpRequestListener listener) {
        this.listener = listener;
    }

    private void setHeaders(Map<String, String> headers) {
        this.headers = headers;
    }
    public static DownloadThread build(){
        return new DownloadThread();
    }

    public DownloadThread entity(DownloadEntity entity){
        setEntity(entity);
        return this;
    }

    public DownloadThread listener(IHttpRequestListener listener){
        setListener(listener);
        return this;
    }

    public DownloadThread headers(Map<String,String> headers){
        setHeaders(headers);
        return this;
    }

    public DownloadThread isSuccess(WorkState isSuccess){
        setIsSuccess(isSuccess);
        return this;
    }
    public DownloadThread reTryNum(int reTryNum){
        setReTryNum(reTryNum);
        return this;
    }

    public DownloadEntity getEntity() {
        return entity;
    }

    public WorkState getIsSuccess() {
        return isSuccess;
    }

    public void setIsSuccess(WorkState isSuccess) {
        this.isSuccess = isSuccess;
    }

    public Map<String, String> getHeaders() {
        return headers;
    }

    private boolean isNull(String content) {
        return content == null || content.trim().length() < 1;
    }


}
